package cnpm02.j2ee.group3.logic.dao.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;

import cnpm02.j2ee.group3.logic.dao.generic.AbstractEntityDaoImpl;
import cnpm02.j2ee.group3.model.Flight;

@Stateless
@Name("flightDao")
@AutoCreate
public class FlightDaoImpl extends AbstractEntityDaoImpl<Flight> implements
		FlightDao {

	@SuppressWarnings("unchecked")
	private List<Flight> getResultList(String strQuery){
		Query query = getEntityManager().createQuery(strQuery);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Flight> selectAll() {
		Query query = getEntityManager().createQuery(SELECT_ALL_QUERY);
		return query.getResultList();
	}

	@Override
	public String delete(final Flight entity){
		if(entity != null){
			entity.setStatus("cancelled");
			getEntityManager().persist(entity);
			
		}
		return "success";
	}

	public List<Flight> selectNewFlight() {
		Query query = getEntityManager().createQuery(SELECT_NEW_FLIGHT);
		query.setParameter("now", new Date());
		return query.getResultList();
	}

	public List<Flight> selectExpiredFlight() {
		Query query = getEntityManager().createQuery(SELECT_EXPIRED_FLIGHT);
		query.setParameter("now", new Date());
		return query.getResultList();
	}

	public List<Flight> selectCancelledFlight() {
		Query query = getEntityManager().createQuery(SELECT_CANCELLED_FLIGHT);
		return query.getResultList();
	}

	public void flush() {
		getEntityManager().flush();
	}
}
